/*
 * B00188409    B00168419   B00210129
 */

package main;
import javax.microedition.lcdui.Image;

import javax.microedition.lcdui.game.Sprite;

public class Jin extends javax.microedition.lcdui.game.Sprite {

    private int RATE = 5;
    protected int speedX = 10;
    protected int speedY = 10;
    private static final int BULLET_SPEED = 1;
    private int HEIGHT  ;
    private int WIDTH ;
    private static final int[] SEQUENCE = {8,7,6,5,4,3,2,1,0};
    private static final int MAX_HP = 100;
    private static final int DAMAGE_RATE =20;
    private int fireTick =0;
    private int current_HP = MAX_HP;
    private int SHOOT_RATE=30;
    private boolean destroyed = false;
    private int damagedTick;

    private Shard shard;
    public Jin(Image image, int w, int h) throws java.io.IOException {
        super(image,w ,h);
        WIDTH = w;
        HEIGHT= h;
        setFrameSequence(SEQUENCE);
        defineReferencePixel(WIDTH/2+1,HEIGHT/2);
        this.setTransform(this.TRANS_MIRROR_ROT270);
    }

    public void initShard(Image image, int w, int h) throws java.io.IOException{
        shard = new Shard(image, w, h );

    }

    public void advance(int ticks) {
        if ((ticks%RATE==0))
            nextFrame();
    }

    public void moveLeft () {
        if (this.getRefPixelX()>0)
            this.move(-speedX, 0);
    }

    public void moveRight (int m) {
        if (this.getRefPixelX() < m)
            this.move(speedX, 0);
    }

    public void moveUp () {
        if (this.getRefPixelY()>0)
            this.move(0, -speedY);
    }

    public void moveDown (int m) {
        if (this.getRefPixelY()<m)
            this.move(0, speedY);
    }

    public Shard fire (int ticks) {

        if (ticks- fireTick > SHOOT_RATE) {
            fireTick = ticks;
            shard.setSpeed( BULLET_SPEED);
            shard.shot(this.getRefPixelX(), this.getRefPixelY()+HEIGHT/2);

            return shard;
        }
        else
            return null;
    }

    public void collided (int ticks, int damage) {
        if (!destroyed)
        if (ticks > damagedTick+DAMAGE_RATE ) {
            current_HP -=damage;
            if (current_HP <= 0) destroyed = true;
            damagedTick = ticks;
        }
    }

    public boolean isDestroyed () {
        return destroyed;
    }

    public double getHPPercentage() {
        if (!destroyed)
            return ((double)current_HP/(1.0*MAX_HP));
        else return 0;
    }

    public boolean isDamageable(int ticks) {
        return (ticks > damagedTick + DAMAGE_RATE);
    }

    public Shard getBullet() {
        return shard;
    }
}